
  var bg = chrome.extension.getBackgroundPage();
  
 
  
  function isHighVersion() {
    var version = navigator.userAgent.match(/Chrome\/(\d+)/)[1];
    return version > 9;
  }
  
  function init() {
    i18nReplace('#optionTitle', 'options');
    i18nReplace('#saveAndClose', 'save_and_close');
    i18nReplace('#screenshootQualitySetting', 'quality_setting');
    i18nReplace('#lossyScreenShot', 'lossy');
    i18nReplace('#losslessScreenShot', 'lossless');
    i18nReplace('#shorcutSetting', 'shortcut_setting');
    i18nReplace('#settingShortcutText', 'shortcutsetting_text');
    i18nReplace('#createAccountLink', 'createAccountLink');
    i18nReplace('#passwordLabel', 'password');
    i18nReplace('#domainLabel', 'domain');
	
	
	
    if (isHighVersion()) {
      $('#lossyScreenShot').html( $('#lossyScreenShot').html()+ ' (JPEG)');
      $('#losslessScreenShot').html($('#losslessScreenShot').html()+' (PNG)');
    }
    
    initScreenCaptureQuality();
    initZeenshareAccount();
    HotKeySetting.setup();
  }

  function save() {
     localStorage.screenshootQuality =
         $('#lossy').val() ? 'jpeg' : '' ||
         $('#lossless').val() ? 'png' : '';
     localStorage.profileName = $('#nameInput').val() ;
     localStorage.pwd = $('#pwdInput').val() ;
     localStorage.domain = $('#domainInput').val() ;
     
     
     return HotKeySetting.save();
  }

  function saveAndClose() {
    if (save())
      chrome.tabs.getSelected(null, function(tab) {
        chrome.tabs.remove(tab.id);
      });
  }

  function initZeenshareAccount() {
	  if(localStorage.profileName) {
		    $('#nameInput').val(localStorage.profileName);
	  }
	  if(localStorage.pwd) {
		    $('#pwdInput').val(localStorage.pwd);
	  }
	  if(localStorage.domain) {
		    $('#domainInput').val(localStorage.domain);
	  }
  }
  function initScreenCaptureQuality() {
    $('#lossy').prop('checked', localStorage.screenshootQuality == 'jpeg');
    $('#lossless').prop('checked', localStorage.screenshootQuality == 'png');
  }

  function i18nReplace(id, name) {
    return $(id).html( chrome.i18n.getMessage(name));
  }



  
    function isWindowsOrLinuxPlatform() {
      return navigator.userAgent.toLowerCase().indexOf('windows') > -1 ||
          navigator.userAgent.toLowerCase().indexOf('linux') > -1;
    }
    function openCreateAccountPopup() {
		window.open("https://zeenshare.com/signup_free",
		"popup",
		"toolbar=no,location=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=1000,height=700");
		
    }

    var HotKeySetting = (function() {
      const CHAR_CODE_OF_AT = 64;
      const CHAR_CODE_OF_A = 65;
      const CHAR_CODE_OF_Z = 90;
      var isWindowsOrLinux = isWindowsOrLinuxPlatform();

      var hotkey = {
        setup: function() {
          // i18n.
          $('#area-capture-text').html(
            chrome.i18n.getMessage('capture_area'));
          $('#viewport-capture-text').html(
            chrome.i18n.getMessage('capture_window'));
          $('#full-page-capture-text').html(
            chrome.i18n.getMessage('capture_webpage'));
          $('#screen-capture-text').html(
            chrome.i18n.getMessage('capture_screen'));

			$('#hot-key-setting select').each(function (index) {
				$(this).append( $('<option></option>').val(CHAR_CODE_OF_AT).html('--'))
	            for (var j = CHAR_CODE_OF_A; j <= CHAR_CODE_OF_Z; j++) {
	              	var value = String.fromCharCode(j);
  					$(this).append( $('<option></option>').val(j).html(value))
	            }				
			});	

          $('#area-capture-hot-key').val(
            HotKey.get('area') );
          $('#viewport-capture-hot-key').val(
            HotKey.get('viewport') );
          $('#full-page-capture-hot-key').val(
            HotKey.get('fullpage') );
          $('#screen-capture-hot-key').val(
            HotKey.get('screen') );

          $('#settingShortcut').click(function() {
            hotkey.setState(this.checked);
          });

          hotkey.setState(HotKey.isEnabled());
          if (isWindowsOrLinux) {
            // Capture screen region is not support on Linux and Mac platform.
            $('#screen-capture-hot-key-set-wrapper').style.display =
                'inline-block';
          }
        },

       

        save: function() {
          var result = true;
          if ($('#settingShortcut').is(':checked')) {
            
              HotKey.enable();
              HotKey.set('area', parseInt($('#area-capture-hot-key').val()));
              HotKey.set('viewport', parseInt($('#viewport-capture-hot-key').val()));
              HotKey.set('fullpage', parseInt($('#full-page-capture-hot-key').val()));

              if (isWindowsOrLinux) {
                var screenCaptureHotKey = parseInt($('#screen-capture-hot-key').val());
                if (bg.plugin.setHotKey(screenCaptureHotKey)) {
                  HotKey.set('screen', screenCaptureHotKey);
                } else {
                  var i18nKey = 'failed_to_register_hot_key_for_screen_capture';
                  ErrorInfo.show(i18nKey);
                  this.focusScreenCapture();
                  result = false;
                }
              }
          } else {
            HotKey.disable(bg);
          }
          return result;
        },

        setState: function(enabled) {
		  $('#settingShortcut').prop('checked', enabled);
		  $('#hot-key-setting').css('color', enabled ? '' : '#6d6d6d');

			$('#hot-key-setting select').each(function (index) {
				$(this).prop('disabled', !enabled);
				}
			);
          ErrorInfo.hide();
        },

        focusScreenCapture: function() {
          $('#screen-capture-hot-key').focus();
        }
      };
      return hotkey;
    })();

    var ErrorInfo = (function() {
      var infoWrapper = $('#error-info');
      return {
        show: function(msgKey) {
          var msg = chrome.i18n.getMessage(msgKey);
          infoWrapper.innerText = msg;
		  infoWrapper.show();
        },

        hide: function() {
          infoWrapper.hide();
        }
      };
    })();
 
 
 

	document.addEventListener("DOMContentLoaded", function() {	
		init();
		$('#createAccountLink').click(function() {
			openCreateAccountPopup();return false;
		});
		$("#saveAndClose").click(function() {
			saveAndClose();return false;
		});
	});